var TableAjax = function () {

    return {

        //main function to initiate the module
        init: function () {
            
            if (!jQuery().dataTable) {
                return;
            }

            $(".modal").on('hidden.bs.modal', function () {
              TableAjax.resetForm();
            });

            // begin first table
            var obTable = $('#sample_1').dataTable({
                "sDom" : "<'row'<'col-md-6 col-sm-12'l><'col-md-12 col-sm-12'f>r>t<'row'<'col-md-5 col-sm-12'i><'col-md-7 col-sm-12'p>>", //default layout without horizontal scroll(remove this setting to enable horizontal scroll for the table)
                "aLengthMenu": [
                    [10, 25, 50, 100, -1],
                    [10, 25, 50, 100, "All"] // change per page values here
                ],
                "bFilter":false,
                "bProcessing": true,
                "bServerSide": true,
                "sAjaxSource": baseUrl+"index.php/informationManage/getList",
                // set the initial value
                "iDisplayLength": 10,
                "sPaginationType": "bootstrap",
                "oLanguage": {
                    "sProcessing": '<i class="fa fa-coffee"></i>&nbsp;Please wait...',
                    "sLengthMenu": "_MENU_ records",
                    "oPaginate": {
                        "sPrevious": "Prev",
                        "sNext": "Next"
                    }
                },
                'bLengthChange':false,
                'fnServerParams': function (aoData) {
                    var data = $('#searchForm').serializeArray();
                    for(var i in data)
                        aoData.push(data[i]);
                }

                // "aoColumnDefs": [{
                //         'bSortable': false,
                //         'aTargets': [0]
                //     }
                // ]
            });

            $('#searchButton').click(function(){
                obTable._fnReDraw();
            });
            $('#searchForm').submit(function(){return false});

            jQuery('#sample_1_wrapper .dataTables_filter input').addClass("form-control input-medium"); // modify table search input
            jQuery('#sample_1_wrapper .dataTables_length select').addClass("form-control input-small"); // modify table per page dropdown

            // handle record edit/remove
            $('body').on('click', '#sample_1_wrapper .btn-editable', function() {
                    window.location.href = "addGoods.html?id="+$(this).attr("data-id");
            });

            $('body').on('click', '#sample_1_wrapper .btn-removable', function() {
                var data_id = $(this).attr("data-id");
                bootbox.confirm("Are you sure?", function(result) {
                    if(!result)return;
                    $.get(baseUrl+"index.php/informationManage/delete",{id:data_id},   // 提交的页面
                        function(data) {
                            var $lmTable = $("#sample_1").dataTable( { bRetrieve : true } );
                            $lmTable.fnDraw();
                        }
                    );
                });
            });

            $('body').on('click', '#sample_1_wrapper .btn-down', function() {
                var data_id = $(this).attr("data-id");
                bootbox.confirm("Are you sure?", function(result) {
                    if(!result)return;
                    $.get(baseUrl+"index.php/orderManage/cancel",{id:data_id},   // 提交的页面
                        function(data) {
                            var $lmTable = $("#sample_1").dataTable( { bRetrieve : true } );
                            $lmTable.fnDraw();
                        }
                    );
                });
            });

            $('.fileUpload').each(function(){
                
                $(this).click(function(){
                    $(this).prev().click();
                });

                var fileName = $(this).next();
                $(this).prev().fileupload({
                        url: baseUrl+"index.php/site/upload",
                        dataType: 'json',
                        autoUpload: true,

                        done: function (e, data) {
                            $.each(data.result.files, function (index, file) {
                                //$('<p/>').text(file.name).appendTo('#files');
                                fileName.val(file.name);
                                fileName.prev().attr("src",baseUrl+"files/"+file.name);
                            });
                        },
                });

            });
             $('#addForm').submit(function(){
                 $.ajax({
                    url: baseUrl+"index.php/goodsManage/add",   // 提交的页面
                    data: $('#addForm').serialize(), // 从表单中获取数据
                    type: "POST",                   // 设置请求类型为"POST"，默认为"GET"
                    error: function(request) {      // 设置表单提交出错
                        alert("表单提交出错，请稍候再试");
                        return false;
                    },
                    success: function(data) {
                        $(".modal").modal("toggle");
                        var $lmTable = $("#sample_1").dataTable( { bRetrieve : true } );
                        $lmTable.fnDraw();

                        }
                    });
                return false;
            });
        },
        resetForm:function(){
            $('#addForm')[0].reset();

            $('.fileUpload').each(function(){
                    $(this).attr("src","assets/fileUpload.jpg");
                });
            }

    };

}();